<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta charset="utf-8"/>
    <title>zlt</title>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/sso.js"></script>
</head>
<body>
<div>
    <p>用户名：<span id="userName"></span></p>
    <p>权限：[<span id="roles"></span>]</p>
    <p>应用id：<span id="clientId"></span></p>
    <p>token：<span id="accessToken"></span></p>
    <p><input type="button" value="登出" onclick="logout()"/></p>
    <button>8001</button>
    <button>8002</button>
    <button>8003</button>
    <button id="hello8001">hello8001</button>
    <div id="result"></div>
</div>
<script>
    window.onload = function() {
        let accessToken = sessionStorage.getItem('access_token');
        if (accessToken) {//已登录
            let username = sessionStorage.getItem('username');
            let roles = sessionStorage.getItem("roles");
            $('#accessToken').html(accessToken);
            $('#userName').html(username);
            $('#roles').html(roles);
            $('#clientId').html(clientId);
        } else {//未登录
            let state = getState();
            sessionStorage.setItem("visitUri", window.location.href);
            sessionStorage.setItem("state", state);
            window.location = getAuthorizeUri(state);
        }
    };

    function logout() {
        let accessToken = sessionStorage.getItem('access_token');
        sessionStorage.removeItem('access_token');
        sessionStorage.removeItem('username');
        sessionStorage.removeItem("roles");
        window.location = getLogoutUri(accessToken);
    }

    $(function () {
        $.ajaxSetup({
            // 发送cookie
            xhrFields: {
                withCredentials: true
            }
        });
        $("#hello8001").click(function () {
            $.ajax({
                dataType: 'json',
                url: "/hello8001",
                success: function (result, status, xhr) {
                    $("#result").html(JSON.stringify(result));
                },
                error: function(x) {
                    console.log(x);
                    $("#result").html('出错了')

                }
            })
        })
        $("button").click(function () {
            let url =  "http://localhost:" + $(this).text();
            console.log(url);
            $.ajax({
                dataType: 'json',
                url: url + "/hello",
                success: function (result, status, xhr) {
                    $("#result").html(JSON.stringify(result));
                },
                error: function(x) {
                    console.log(x);
                    $("#result").html('出错了')

                }
            })
        })
    })
</script>
</body>
</html>
